/*!
 * copyright c by zhangxinxu 2012-02-06
 * jquery.placeholder.js placeholder灞炴€фā鎷熸彃浠�
 * v1.0 2012-02-06 create
 * v1.2 2012-12-20 鍏煎IE10涓婭E6-IE7锛屽悓鏃秊Query1.4.*鐗堟湰
 * v1.3 2013-06-14 label鍏冪礌margin/padding鏃犳晥闂
*/

(function($, undefined) {
    $.fn.placeholder = function(options) {
        var defaults = {
            labelMode: false,
            labelStyle: {},
            labelAlpha: false,
            labelAcross: false
        };
        var params = $.extend({}, defaults, options || {});
        
        //鏂规硶
        var funLabelAlpha = function(elementEditable, elementCreateLabel) {
            if (elementEditable.val() === "") {
                elementCreateLabel.css("opacity", 1).html(elementEditable.data("placeholder"));
            } else {
                elementCreateLabel.html("");    
            }
        }, funPlaceholder = function(ele) {
            // 涓轰簡鍚戜笅鍏煎jQuery 1.4
            if (document.querySelector) {
                return $(ele).attr("placeholder");  
            } else {
                // IE6, 7
                var ret;
                ret = ele.getAttributeNode("placeholder");
                // Return undefined if nodeValue is empty string
                return ret && ret.nodeValue !== "" ? ret.nodeValue : undefined; 
            }
        };
        
        $(this).each(function() {
            var element = $(this), isPlaceholder = "placeholder" in document.createElement("input"), placeholder = funPlaceholder(this);
            
            // 涓夌鎯呭喌鎵撻叡娌�
            // 鈶� 娌℃湁placeholder灞炴€у€�
            // 鈶� value妯℃嫙锛屽悓鏃舵槸鏀寔placeholder灞炴€х殑娴忚鍣�
            // 鈶� label妯℃嫙锛屼絾鏄棤闇€璺ㄦ祻瑙堝櫒鍏煎锛屽悓鏃舵槸鏀寔placeholder灞炴€х殑娴忚鍣�
            if (!placeholder || (!params.labelMode && isPlaceholder) || (params.labelMode && !params.labelAcross && isPlaceholder)) { return; }

            // 瀛樺偍锛屽洜涓烘湁鏃朵細娓呴櫎placeholder灞炴€�
            element.data("placeholder", placeholder);
            
            // label妯℃嫙
            if (params.labelMode) {         
                var idElement = element.attr("id"), elementLabel = null;
                if (!idElement) {
                    idElement = "placeholder" + Math.random();  
                    element.attr("id", idElement);
                }
                
                // 鐘舵€佸垵濮嬪寲
                elementLabel = $('<label for="'+ idElement +'"></label>').css($.extend({
                    lineHeight: "1.3",
                    position: "absolute",
                    color: "graytext",
                    cursor: "text",
                    marginLeft: element.css("marginLeft"),
                    marginTop: element.css("marginTop"),
                    paddingLeft: element.css("paddingLeft"),
                    paddingTop: element.css("paddingTop")
                }, params.labelStyle)).insertBefore(element);               
                
                // 浜嬩欢缁戝畾
                if (params.labelAlpha) {
                    // 濡傛灉鏄负绌篺ocus閫忔槑搴︽敼鍙樹氦浜�
                    element.bind({
                        "focus": function() {
                            funLabelAlpha($(this), elementLabel);
                        },
                        "input": function() {
                            funLabelAlpha($(this), elementLabel);
                        },
                        "blur": function() {
                            if (this.value === "") {
                                elementLabel.css("opacity", 1).html(placeholder);  
                            }   
                        }
                    }); 
                    
                    //IE6~8涓嶆敮鎸乷ninput浜嬩欢锛岄渶瑕佸彟琛岀粦瀹�
                    if (!window.screenX) {
                        element.get(0).onpropertychange = function(event) {
                            event = event || window.event;
                            if (event.propertyName == "value") {
                                funLabelAlpha(element, elementLabel);   
                            };  
                        }
                    }
                    
                    // 鍙抽敭浜嬩欢
                    elementLabel.get(0).oncontextmenu = function() {
                        element.trigger("focus");
                        return false;   
                    }
                } else {
                    //濡傛灉鏄崟绾殑value浜や簰
                    element.bind({
                        "focus": function() {
                            elementLabel.html("");
                        },
                        "blur": function() {
                            if ($(this).val() === "") {
                                elementLabel.html(placeholder); 
                            }
                        }
                    }); 
                }
                
                // 鍐呭鍒濆鍖�
                if (params.labelAcross) {
                    element.removeAttr("placeholder");  
                }
                
                if (element.val() === "") {
                    elementLabel.html(placeholder); 
                }
            } else {
                // value妯℃嫙
                element.bind({
                    "focus": function() {
                        if ($(this).val() === placeholder) {
                            $(this).val("");
                        }
                        $(this).css("color", "");   
                    },
                    "blur": function() {
                        if ($(this).val() === "") {
                            $(this).val(placeholder).css("color", "graytext");    
                        }   
                    }
                }); 
                
                // 鍒濆鍖�
                if (element.val() === "") {
                    element.val(placeholder).css("color", "graytext");      
                }
            }   
        });
        return $(this);
    };  
})(jQuery);